package com.yangle.frame.admin.controller;

import com.yangle.frame.admin.entity.BaseEntity;
import com.yangle.frame.admin.entity.FrameTable;
import com.yangle.frame.admin.service.FrameTableService;
import com.yangle.frame.common.controller.BaseController;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import tk.mybatis.mapper.util.StringUtil;
import java.util.UUID;

/**
 * Created by yangle on 2018/5/26.
 */
@Controller
@RequestMapping("/frame/frametable")
public class FrameTableController extends BaseController<FrameTable>{
    @Autowired
    private FrameTableService FrameTableService;
    @RequestMapping("/list")
    public String list(HttpServletRequest request, FrameTable entity) {
        super.list(request, entity, FrameTableService);
        request.setAttribute("tableId",entity.getTableId());
        return "frame/frametable/list";
    }

    @RequestMapping("/edit")
    public String edit(HttpServletRequest request) {


        if(StringUtil.isEmpty(request.getParameter("id"))){
        FrameTable frameTable=new FrameTable();
        frameTable.setTableId(request.getParameter("tableId"));
        request.setAttribute("entity",frameTable);}else{
            super.edit(request, FrameTableService);
        }
        return "frame/frametable/edit";
    }

    @RequestMapping("/save")
    public String save(HttpServletRequest request, FrameTable entity) {
        //执行字段的操作

        FrameTableService.createOrAlterField(entity);
        if (StringUtil.isNotEmpty(entity.getId())) {
            super.update(request, entity, FrameTableService);
        } else {
            entity.setId(UUID.randomUUID().toString());
            super.create(request, entity, FrameTableService);
        }
        return "common/result";
    }

    @RequestMapping("/delete")
    public String delete(HttpServletRequest request) {
        FrameTableService.dropField(request.getParameter("ids"));
        super.delete(request, FrameTableService);
        return "common/result";
    }

    @RequestMapping("/view")
    public String view(HttpServletRequest request) {
        super.view(request, FrameTableService);
        return "frame/frametable/view";
    }
    @ResponseBody
    @RequestMapping("/createTable")
    public String createTable(String tableName){
        FrameTableService.createTable(tableName);
        return "创建成功";
    }
    @ResponseBody
    @RequestMapping("/queryTableIsExists")
    public int queryTableIsExists(String tableName){
        return FrameTableService.queryTableIsExists(tableName);

    }
    @ResponseBody
    @RequestMapping("/queryFieldIsExists")
    public int queryFieldIsExists(String tableName,String fieldName){
        return FrameTableService.queryFieldIsExists(tableName,fieldName);

    }

}